//
//  PubRunVC.swift
//  BUYOU
//
//  Created by tyuu on 2024/5/29.
//

import UIKit

class PubRunVC: BaseViewController {
    
    let scrollView = UIScrollView()
    let contentView = UIView()
    
    
    let exercises = [
        ("自由跑", "run_image", "startRun"),
        ("仰卧起坐", "situp_image", "startSitup"),
        ("俯卧撑", "pushup_image", "startPushup"),
        ("跳绳", "jumprope_image", "startJumpRope")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        navigationItem.title = "开始运动"
        
        setupExerciseButtons()
    }
    
    // 动态生成运动按钮和图片
    private func setupExerciseButtons() {
        
        view.addSubview(scrollView)
        scrollView.addSubview(contentView)
        
        scrollView.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.bottom.equalTo(view.safeAreaLayoutGuide)
        }
        
        contentView.snp.makeConstraints { make in
            make.edges.equalTo(scrollView)
            make.width.equalTo(scrollView)
            make.height.equalTo(800)
        }
        
        var lastView: UIView?
        
        for (name, imageName, selector) in exercises {
            let containerView = UIView()
            contentView.addSubview(containerView)
            
            let imageView = UIImageView()
            imageView.contentMode = .scaleAspectFit
            imageView.image = UIImage(named: imageName)
            containerView.addSubview(imageView)
            
            let button = UIButton(type: .system)
            button.setTitle("开始 \(name)", for: .normal)
            button.setTitleColor(.white, for: .normal)
            button.backgroundColor = .blue
            button.layer.cornerRadius = 5
            button.clipsToBounds = true
            button.addTarget(self, action: Selector(selector), for: .touchUpInside)
            containerView.addSubview(button)
            
            containerView.snp.makeConstraints { make in
                make.left.right.equalTo(view).inset(16)
                if let lastView = lastView {
                    make.top.equalTo(lastView.snp.bottom).offset(16)
                } else {
                    make.top.equalToSuperview().offset(16)
                }
                make.height.equalTo(150)
            }
            
            imageView.snp.makeConstraints { make in
                make.top.left.right.equalTo(containerView)
                make.height.equalTo(100)
            }
            
            button.snp.makeConstraints { make in
                make.top.equalTo(imageView.snp.bottom).offset(8)
                make.left.right.bottom.equalTo(containerView).inset(16)
                make.height.equalTo(40)
            }
            
            lastView = containerView
        }
    }
    
    // 运动开始方法
    @objc private func startRun() {
        startExercise(type: "自由跑")
    }
    
    @objc private func startSitup() {
        startExercise(type: "仰卧起坐")
    }
    
    @objc private func startPushup() {
        startExercise(type: "俯卧撑")
    }
    
    @objc private func startFrogJump() {
        startExercise(type: "蛙跳")
    }
    
    @objc private func startJumpRope() {
        startExercise(type: "跳绳")
    }
    
    // 根据运动类型开启不同功能
    private func startExercise(type: String) {
        
        let workoutViewController = StartWorkoutViewController(workoutType: type)
        present(workoutViewController, animated: true, completion: nil)

    }
    
    
    
}
